<?php
// Script to display a browse PHP code documentation using the Mdoc class
// Author:  JM Faure <jmfaure@users.sourceforge.net>
// Release: 19 Sep 2005
//
// Usage: browse the whole code documentation from this page,
//        ? (no query) view documentation for a whole directory
//        ?lib=libname view the requested library documentation
//        ?lib=libname?constants view the requested all-constants documentation
//        ?lib=libname?constant=constname view the requested constant documentation
//        ?lib=libname?constant_group=0 view the requested constant_group documentation
//        ?class=classname view the requested class documentation
//        ?class=classname&method=methodname view the requested method documentation
//        ?class=classname&property=propertyname view the requested property documentation
//        ?lib=libname&function=functionname view the requested function documentation
//
// The page is made from 4 elements:
//        header (optional, set in config)
//        toc (built with Mdoc)
//        page (built with Mdoc)
//        footer (optional, set in config)

// Timing script execution
$time_start = microtime();

// Load local setup
require("./browse-config.php");

// Load required Manta classes
if ($force_php4 
    || version_compare(phpversion(), "5") < 0) {
  $manta_lib_path = dirname(__FILE__) . "/../include/php4/";
} else {
  $manta_lib_path = dirname(__FILE__) . "/../include/php5/";
}
require($manta_lib_path . "Mdoc.php");

// Set view level
if (!empty($_GET['view']) && 
    in_array($_GET['view'], array("author", "public"))) {
  $view_level = $_GET['view'];
} else {
  $view_level = $default_view;
}

// Set current (requested) directory
if (!empty($_GET['path']) && $_GET['path'] == "manta") {
   $current_dir = $manta_lib_path;
} else {
   $current_dir = dirname(__FILE__) . "/../../oh.framework/";  // default samples path
}

// Set parameters to process the request
// -- lib, class or directory?
if (!empty($_GET['lib'])) {
  $type = 'lib';
  $file = $_GET['lib'];
  $file_path = $current_dir . $file . $php_ext;
  if (!file_exists($file_path)) {
echo $file_path;
echo dirname(__FILE__);
    die("INVALID REQUEST : REQUESTED FILE DOESN'T EXIST");
  }
} elseif (!empty($_GET['class'])) {
  $type = 'class';
  $file = $_GET['class'];
  $file_path = $current_dir . $file . $php_ext;
  if (!file_exists($file_path)) {
    die("INVALID REQUEST : REQUESTED FILE DOESN'T EXIST");
  }
// -- not lib, nor class so is a directory request
} else {
  $type = 'home';
}
// -- method, function or property?
if ($type != 'home') {
  if (array_key_exists('constants', $_GET)) {
    $constants = true;
  } elseif (!empty($_GET['method'])) {
    $method = $_GET['method'];
  } elseif (!empty($_GET['constant'])) {
    $constant = $_GET['constant'];
  } elseif (array_key_exists('constant_group', $_GET)) {
    $constant_group = $_GET['constant_group'];
  } elseif (!empty($_GET['function'])) {
    $function = $_GET['function'];
  } elseif (!empty($_GET['property'])) {
    $property = $_GET['property'];
  }
}

// Buid requested manual page
if ($type == 'home') {
  // Manual home
  $toc = 
     "<ul id=\"manualtoc\">\n" .
     "<li class=\"header home\"><a href=\"/index.php\">Home</a></li>\n" .
     "<li class=\"header up\"><a href=\"../index.php\">phpManta suite</a></li>\n" .
     "<li><a href=\"?view=$view_level\">Mdoc in action</a></li>\n" .
     "</ul>\n";  
  $page = 
     "<p>Browse Mdoc class and library samples (<a href=\"?view=author\">set author view</a> or <a href=\"?view=public\">set public view</a>): </p>\n" .
     "<ul>\n" .
     "  <li>Class sample: <a href=\"?class=Abstract1&view=$view_level\">abstract</a>,\n" .
     "                    <a href=\"?class=Interface1&view=$view_level\">interface</a>,\n" . 
     "                    <a href=\"?class=Final1&view=$view_level\">final class </a>,\n" . 
     "                    <a href=\"?class=Class1&view=$view_level\">class</a>,\n" . 
     "                    <a href=\"?class=Class1&amp;method=mpbyref&view=$view_level\">method</a>,\n" . 
     "                    <a href=\"?class=Class1&amp;property=multi&view=$view_level\">property</a>,\n" .
     "                    <a href=\"?class=Class3&amp;constant=Class3::START&view=$view_level\">constant</a>,\n" . 
     "                    <a href=\"?class=Class3&amp;constant_group=1&view=$view_level\">constant_group</a>,\n" . 
     "                    <a href=\"?class=Class3&amp;constants&view=$view_level\">all constants</a></li>\n" .
     "  <li>Library sample: <a href=\"?lib=Lib1&view=$view_level\">library</a>,\n" . 
     "                      <a href=\"?lib=Lib1&amp;function=f3param&view=$view_level\">function</a>,\n" . 
     "                      <a href=\"?lib=Lib1&amp;constant=LIBT_ERRMSG_1&view=$view_level\">constant</a>,\n" . 
     "                      <a href=\"?lib=Lib1&amp;constant_group=1&view=$view_level\">constant_group</a>,\n" . 
     "                      <a href=\"?lib=Lib1&amp;constants&view=$view_level\">all constants</a></li>\n" .
     "</ul>\n" .
     "<p><a href=\"auto-documentation.php\">View Mdoc in action on itself.</a></p>\n";
} else {
  // Create the manual
  $manual = new Mdoc($file_path);
  // Get the TOC
  if ($type == 'class') {
    $toc = $manual->getnav_class($view_level);
  } elseif ($type == 'lib') {
    $toc = $manual->getnav_library($view_level);
  } else {
    die("INVALID REQUEST : UNEXPECTED SITUATION ENCOUNTERED");
  }
  // Get the documentation
  if (!empty($constants)) {
    $page = $manual->getdoc_constants($view_level);
  } elseif (!empty($constant)) {
    $page = $manual->getdoc_constant($constant, $view_level);
  } elseif (isset($constant_group) && is_numeric($constant_group)) {
    $page = $manual->getdoc_constant_group($constant_group, $view_level);
  } elseif (!empty($method)) {
    $page = $manual->getdoc_method($method, $view_level);
  } elseif (!empty($function)) {
    $page = $manual->getdoc_function($function, $view_level);
  } elseif (!empty($property)) {
    $page = $manual->getdoc_property($property, $view_level);
  // -- so is to document the whole class (or lib) 
  } elseif ($type == 'class') {
    $page = $manual->getdoc_class($view_level);
  } elseif ($type == 'lib') {
    $page = $manual->getdoc_library($view_level);
  } else {
    die("INVALID REQUEST : UNEXPECTED SITUATION ENCOUNTERED");
  }
}

// Compute script execution time before output
$time_end = microtime();
list($usec, $sec) = explode(" ", $time_end);
$time_end = (float)$usec + (float)$sec;
list($usec, $sec) = explode(" ", $time_start);
$time_start = (float)$usec + (float)$sec;
$time_script = number_format ($time_end - $time_start, 3);

// Add header and footer in the big table
$header = empty($header) ? "" : 
          "<tr>\n    <td class=\"header\" colspan=\"3\">$header</td>\n  </tr>";
$footer = empty($footer) ? "" :
          "<tr>\n    <td class=\"footer\" colspan=\"3\">$footer\n" .
          "      <br /><div class=\"subfooter\">Execution time $time_script seconds</span></td>\n  </tr>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpManta - Browse PHP code documentation with Mdoc</title>
<link href="browse-style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <? echo $header; ?>
  <tr valign="top">
    <td width="200" bgcolor="#F0F0F0"><table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tr valign="top">
          <td class="sidebar"><? echo $toc; ?> </td>
        </tr>
      </table></td>
    <td bgcolor="#CCCCCC" background="images/checkerboard.gif" width="1">
      <img src="images/spacer.gif" width="1" height="1" border="0" alt="" /></td>
    <td><table cellpadding="10" cellspacing="0" width="100%">
        <tr>
          <td valign="top"><? echo $page; ?> </td>
        </tr>
      </table></td>
  </tr>
  <? echo $footer; ?>
</table>
</body>
</html>
